<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="check">查看信息</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
        </script>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="/static/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script>
    layui.use(['layer', 'table'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.$;
        //第一个实例
        table.render({
            elem: '#currentTableId'
            , toolbar: '#toolbarDemo'
            , limit: 20
            , url: '{:url("data/gettotalmp")}'
            , page: true
            , cols: [[
                {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'}
                , {field: 'mp_name', title: '公众号名称'}
                , {field: 'mp_user', title: '公众号账号'}
                , {
                    field: 'state', title: '状态', templet: function (d) {
                        if (d.state == 1) {
                            return '<span class="layui-badge layui-bg-cyan">对接成功</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-red">对接失败</span>';
                        }
                    }
                }
                , {title: '操作', toolbar: '#barDemo'},
            ]]
        });
        //触发头部事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event == 'add') {
                var index = layer.open({
                    title: '添加账号',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '70%'],
                    content: "{:url('api/mp/add')}",
                });
            }
        });
        //工具条事件
        table.on('tool(currentTableFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'check') { //查看
                var index = layer.open({
                    title: '详细信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: "{:url('api/mp/check')}?id=" + data.id
                });
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '详细信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: "{:url('api/mp/edit')}?id=" + data.id
                });

            } else if (layEvent === 'del') { //删除

                layer.confirm('真的删除吗？', {icon: 2, skin: 'layui-layer-admin-modal'}, function (index) {
                    $.ajax({
                        type: "post",
                        url: "{:url('api/mp/del')}",
                        data: 'id=' + data.id,
                        dataType: 'json',
                        success: function (res) {
                            if (res.code == 1) {
                                obj.del();
                                layer.close(index);
                                layer.msg(res.msg);
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    });
                });
            }


        });


    });
</script>
</body>

</html>